package com.gss.module.generator.core;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.util.StrUtil;
import com.gss.common.utils.StringUtils;
import com.gss.module.generator.constants.CommonConstants;
import com.gss.module.generator.entity.PomEntity;

import java.util.List;

/**
 * @author DYang
 * @date 2023/5/30
 * pom.xml文件生成器
 */
public class PomXmlGenerator {

    private static  String POM_PATH = "classpath:template/PomXmlGeneratorTemplate.template";

    /**
     * 根据内如构建pom.xml文件
     * @param pomEntity
     */
    public static void builder(PomEntity pomEntity,String projectPath){
        //构建实体类
        String content = buildXmlInfo(pomEntity,POM_PATH);
        if (content == null){
            System.out.println("模板不存在，暂未解析到数据");
            return;
        }
        //存储到指定指定文件
        saveEntityFile(content,projectPath);
    }

    /**
     * 构建pom.xml信息
     * @param pomEntity
     * @param pomPath
     * @return
     */
    private static String buildXmlInfo(PomEntity pomEntity, String pomPath) {
        String content = ResourceUtil.readUtf8Str(pomPath);
        if (StringUtils.isEmpty(content)) {
            return null;
        }

        String parentDependency = pomEntity.getParentDependency() == null ? "" : pomEntity.getParentDependency();
        String version = buildVersion(pomEntity);
        String dependencyManagement = buildDependencyManagement(pomEntity.getDependencyList());
        String dependencies = buildDependencies(pomEntity.getDependencyList());

        content = content.replace("${PARENT_DEPENDENCY}",parentDependency);
        content = content.replace("${GROUP_ID}", pomEntity.getGroupId());
        content = content.replace("${ARTIFACT_ID}",pomEntity.getArtifactId());
        content = content.replace("${DEPENDENCY_VERSION}",version);
        content = content.replace("${DEPENDENCY_MANAGEMENT}",dependencyManagement);
        content = content.replace("${DEPENDENCIES}",dependencies);
        return content;
    }

    /**
     * 版本管理
     * @param pomEntity
     * @return
     */
    private static String buildVersion(PomEntity pomEntity) {
        List<PomEntity> dependencyList = pomEntity.getDependencyList();
        if (CollUtil.isEmpty(dependencyList)) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (PomEntity entity : dependencyList) {
            String version = entity.getVersion();
            String versionName = entity.getVersionName();
            if (StrUtil.isEmpty(version)) {
                buffer.append("        <"+ versionName +">" + version + "</"+ versionName +">\n");
            }
        }
        if (buffer.length() == 0) {
            return "";
        }
        return buffer.toString();
    }

    /**
     * 依赖管理构建
     * @param dependencyList
     * @return
     */
    private static String buildDependencyManagement(List<PomEntity> dependencyList) {
        if (CollUtil.isEmpty(dependencyList)) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (PomEntity entity : dependencyList) {
            buffer.append("            <dependency>\n");
            buffer.append("                 <groupId>"+ entity.getGroupId() +"</groupId>\n");
            buffer.append("                 <artifactId>"+ entity.getArtifactId() +"</artifactId>\n");
            buffer.append("                 <version>${"+ entity.getVersionName() +"}</version>\n");
            buffer.append("            </dependency>\n");
        }
        return buffer.toString();
    }

    /**
     * 构建依赖
     * @param dependencyList
     * @return
     */
    private static String buildDependencies(List<PomEntity> dependencyList) {
        if (CollUtil.isEmpty(dependencyList)) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (PomEntity entity : dependencyList) {
            boolean enable = entity.isEnable();
            if (enable) {
                buffer.append("            <dependency>\n");
                buffer.append("                 <groupId>"+ entity.getGroupId() +"</groupId>\n");
                buffer.append("                 <artifactId>"+ entity.getArtifactId() +"</artifactId>\n");
                buffer.append("            </dependency>\n");
            }
        }
        return buffer.toString();
    }


    /**
     * 将数据写道指定文件
     * @param content
     * @param projectPath
     */
    private static void saveEntityFile(String content,String projectPath) {
        projectPath = projectPath + "/pom." + CommonConstants.XML_FILE;
        FileUtil.writeString(content, projectPath, "UTF-8");
    }
}
